

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include <wx/caret.h>
#include <wx/clipbrd.h>
#include <wx/event.h>
#include <wx/srchctrl.h>
#include <wx/dcbuffer.h>

#include "dgt_FilterCtrl.h"

#ifdef __WXMAC__

/**********************************************************************************************/
dgtFilterCtrl::dgtFilterCtrl(wxWindow* parent, 
						   wxWindowID id,
						   wxString value,
						   const wxPoint& pos, 
						   const wxSize& size,
						   long style)
{
	Create(parent, id, value, pos, size, style);
	ShowSearchButton( true );
	SetWindowStyleFlag( wxNO_BORDER );
}

#else //__WXMAC__

/* XPM */
static char * left_xpm[] = {
	"20 22 95 2",
	"  	c None",
	". 	c #B4B4B4",
	"+ 	c #9B9B9B",
	"@ 	c #888888",
	"# 	c #787878",
	"$ 	c #767676",
	"% 	c #777777",
	"& 	c #AFAFAF",
	"* 	c #909090",
	"= 	c #949494",
	"- 	c #A8A8A8",
	"; 	c #B7B7B7",
	"> 	c #C0C0C0",
	", 	c #C5C5C5",
	"' 	c #C7C7C7",
	") 	c #9F9F9F",
	"! 	c #BCBCBC",
	"~ 	c #D3D3D3",
	"{ 	c #E0E0E0",
	"] 	c #E9E9E9",
	"^ 	c #EDEDED",
	"/ 	c #F0F0F0",
	"( 	c #F1F1F1",
	"_ 	c #FFFFFF",
	": 	c #9E9E9E",
	"< 	c #ADADAD",
	"[ 	c #E8E8E8",
	"} 	c #F3F3F3",
	"| 	c #F8F8F8",
	"1 	c #FBFBFB",
	"2 	c #FDFDFD",
	"3 	c #FEFEFE",
	"4 	c #A4A4A4",
	"5 	c #B3B3B3",
	"6 	c #DCDCDC",
	"7 	c #FAFAFA",
	"8 	c #AAAAAA",
	"9 	c #F2F2F2",
	"0 	c #989898",
	"a 	c #737373",
	"b 	c #999999",
	"c 	c #A5A5A5",
	"d 	c #D0D0D0",
	"e 	c #B1B1B1",
	"f 	c #606060",
	"g 	c #5D5D5D",
	"h 	c #5B5B5B",
	"i 	c #BFBFBF",
	"j 	c #F9F9F9",
	"k 	c #5E5E5E",
	"l 	c #696969",
	"m 	c #CCCCCC",
	"n 	c #939393",
	"o 	c #5A5A5A",
	"p 	c #A1A1A1",
	"q 	c #EFEFEF",
	"r 	c #F5F5F5",
	"s 	c #EEEEEE",
	"t 	c #ACACAC",
	"u 	c #FCFCFC",
	"v 	c #6B6B6B",
	"w 	c #595959",
	"x 	c #D8D8D8",
	"y 	c #A9A9A9",
	"z 	c #E7E7E7",
	"A 	c #DEDEDE",
	"B 	c #AEAEAE",
	"C 	c #EBEBEB",
	"D 	c #8B8B8B",
	"E 	c #B8B8B8",
	"F 	c #B9B9B9",
	"G 	c #6E6E6E",
	"H 	c #B2B2B2",
	"I 	c #E6E6E6",
	"J 	c #C3C3C3",
	"K 	c #A0A0A0",
	"L 	c #616161",
	"M 	c #797979",
	"N 	c #CECECE",
	"O 	c #676767",
	"P 	c #707070",
	"Q 	c #7D7D7D",
	"R 	c #E5E5E5",
	"S 	c #EAEAEA",
	"T 	c #5C5C5C",
	"U 	c #CDCDCD",
	"V 	c #F6F6F6",
	"W 	c #CFCFCF",
	"X 	c #F7F7F7",
	"Y 	c #BABABA",
	"Z 	c #D5D5D5",
	"` 	c #E4E4E4",
	" .	c #D7D7D7",
	"..	c #DBDBDB",
	"+.	c #D6D6D6",
	"              . + @ # $ % % % % % % % % ",
	"          & * = - ; > , ' ' ' ' ' ' ' ' ",
	"        ) + ! ~ { ] ^ / ( ( ( ( ( ( _ _ ",
	"      : < ~ [ } | 1 2 3 3 3 3 3 3 3 _ _ ",
	"    4 5 6 / 7 2 3 _ _ _ _ _ _ _ _ _ _ _ ",
	"  ; 8 6 9 1 2 ' 0 a a b ' 3 _ _ _ _ _ _ ",
	"  c d / 1 3 e f g h h g f e 3 _ _ _ _ _ ",
	"i . [ j 3 ' k l ) m m ) l k ' _ _ _ _ _ ",
	"e m } 2 2 n o p q r r s p o n 2 _ _ _ _ ",
	"t 6 j 3 u v w x 1 3 3 7 x w v 2 _ _ _ _ ",
	"y z 1 _ 1 l w A 3 _ _ 3 A w l u _ _ _ _ ",
	"B C u _ u D w B 3 _ _ 3 B w D 1 _ _ _ _ ",
	"E [ u _ 2 F w G H I I e G w H u _ _ _ _ ",
	"J { 1 _ 2 ( K o k L L k o w M / _ _ _ _ ",
	"N ~ j 3 _ 1 s 5 @ O O @ 8 P h Q ( _ _ _ ",
	"  ' q 2 _ _ u ( ] I R S ^ 6 P T Q 9 _ _ ",
	"  U 6 j 3 _ _ 2 u 1 1 u 3 V 6 P k m _ _ ",
	"    W [ 1 3 _ _ _ _ _ _ _ 3 X 6 Y r 3 _ ",
	"      Z S 1 3 _ _ _ _ _ _ _ 3 j V 2 _ _ ",
	"        ~ ` V 2 3 _ _ _ _ _ _ _ 3 _ _ _ ",
	"          Z  .R q X u 3 3 3 3 3 3 3 3 3 ",
	"              ..x +.Z x x x x x x x x x "};

/* XPM */
static char * right_xpm[] = {
	"15 22 65 1",
	" 	c None",
	".	c #777777",
	"+	c #767676",
	"@	c #787878",
	"#	c #888888",
	"$	c #9B9B9B",
	"%	c #B4B4B4",
	"&	c #C7C7C7",
	"*	c #C5C5C5",
	"=	c #C0C0C0",
	"-	c #B7B7B7",
	";	c #A8A8A8",
	">	c #949494",
	",	c #909090",
	"'	c #AFAFAF",
	")	c #F1F1F1",
	"!	c #F0F0F0",
	"~	c #EDEDED",
	"{	c #E9E9E9",
	"]	c #E0E0E0",
	"^	c #D3D3D3",
	"/	c #BCBCBC",
	"(	c #9F9F9F",
	"_	c #FEFEFE",
	":	c #FDFDFD",
	"<	c #FBFBFB",
	"[	c #F8F8F8",
	"}	c #F3F3F3",
	"|	c #E8E8E8",
	"1	c #ADADAD",
	"2	c #9E9E9E",
	"3	c #FFFFFF",
	"4	c #FAFAFA",
	"5	c #DCDCDC",
	"6	c #B3B3B3",
	"7	c #A4A4A4",
	"8	c #F2F2F2",
	"9	c #AAAAAA",
	"0	c #D0D0D0",
	"a	c #F9F9F9",
	"b	c #BEBEBE",
	"c	c #CCCCCC",
	"d	c #B1B1B1",
	"e	c #ACACAC",
	"f	c #E7E7E7",
	"g	c #A9A9A9",
	"h	c #FCFCFC",
	"i	c #EBEBEB",
	"j	c #AEAEAE",
	"k	c #B8B8B8",
	"l	c #C3C3C3",
	"m	c #CECECE",
	"n	c #EFEFEF",
	"o	c #C8C8C8",
	"p	c #CFCFCF",
	"q	c #EAEAEA",
	"r	c #D5D5D5",
	"s	c #F6F6F6",
	"t	c #E4E4E4",
	"u	c #F7F7F7",
	"v	c #E5E5E5",
	"w	c #D7D7D7",
	"x	c #D8D8D8",
	"y	c #D6D6D6",
	"z	c #DBDBDB",
	"...+@#$%       ",
	"&&&*=-;>,'     ",
	")))!~{]^/$(    ",
	"____:<[}|^12   ",
	"333333_:4!567  ",
	"33333333_<859- ",
	"333333333_<!07 ",
	"3333333333_a|%b",
	"33333333333:}cd",
	"33333333333_a5e",
	"333333333333<fg",
	"333333333333hij",
	"333333333333h|k",
	"333333333333<]l",
	"33333333333_a^m",
	"33333333333:no ",
	"3333333333_a5m ",
	"333333333_<|p  ",
	"33333333_<qr   ",
	"333333_:st^    ",
	"____hunvwr     ",
	"xxxxryxz       "};

BEGIN_EVENT_TABLE( dgtFilterCtrl, wxPanel )
	EVT_PAINT( dgtFilterCtrl::OnPaint )
	EVT_ERASE_BACKGROUND( dgtFilterCtrl::OnEraseBackground )
	EVT_LEFT_DOWN( dgtFilterCtrl::OnLeftDown )
END_EVENT_TABLE()

/**********************************************************************************************/
dgtFilterCtrl::dgtFilterCtrl(wxWindow* parent, 
						   wxWindowID id,
						   const wxPoint& pos, 
						   const wxSize& size,
						   long style)
: m_iId( id )
, m_bmpLeft( left_xpm )
, m_bmpRight( right_xpm )
, m_pMenu( NULL )
, m_bGradient( false )
{
//	argused1( style );

	Create( parent, -1, pos, size, wxNO_BORDER );

	wxBoxSizer* t_pSizer = new wxBoxSizer( wxHORIZONTAL );
	SetSizer( t_pSizer );

	m_pTextCtrl = new wxTextCtrl( this, id, wxEmptyString, wxDefaultPosition, wxSize( -1, 15 ), wxTE_PROCESS_ENTER|wxNO_BORDER );
	
	t_pSizer->AddSpacer( 20 );
	t_pSizer->Add( m_pTextCtrl, 1, wxALIGN_CENTER_VERTICAL|wxUP|wxDOWN, 4 );
	t_pSizer->AddSpacer( 10 );
}

/**********************************************************************************************/
void dgtFilterCtrl::OnPaint( wxPaintEvent& event )
{
//	argused1( event );

	wxBufferedPaintDC dc( this );

	wxColour cl = GetBackgroundColour();
	wxRect rect( 0, 0, GetSize().GetX(), GetSize().GetY() );

	if( !m_bGradient )
	{
		dc.SetBrush( cl );
		dc.SetPen( cl );
		dc.DrawRectangle( rect );
	}
	else
	{
		dc.GradientFillLinear( rect, wxColour( 252, 252, 252 ), wxColour( 237, 237, 237 ), wxSOUTH );
	}

	dc.DrawBitmap( m_bmpLeft, 0, 0, true );
	dc.DrawBitmap( m_bmpRight, rect.GetRight() - m_bmpRight.GetWidth() + 1, 0, true );

	rect.SetLeft( m_bmpLeft.GetWidth() );
	rect.SetWidth( rect.GetWidth() - m_bmpLeft.GetWidth() - m_bmpRight.GetWidth() );
	rect.SetHeight( rect.GetHeight() - 1 );

	dc.SetBrush( *wxWHITE );
	dc.SetPen( *wxWHITE );
	dc.DrawRectangle( rect );

	rect.SetWidth( rect.GetWidth() + 1 );

	dc.SetPen( wxColour( 119, 119, 119 ) );
	dc.DrawLine( rect.GetTopLeft(), rect.GetTopRight() );

	dc.SetPen( wxColour( 199, 199, 199 ) );
	dc.DrawLine( rect.GetTopLeft() + wxPoint( 0, 1 ), rect.GetTopRight() + wxPoint( 0, 1 ) );

	dc.SetPen( wxColour( 216, 216, 216 ) );
	dc.DrawLine( rect.GetBottomLeft(), rect.GetBottomRight() );
}

/**********************************************************************************************/
void dgtFilterCtrl::OnEraseBackground( wxEraseEvent& event )
{
	//argused1( event );
}

/**********************************************************************************************/
void dgtFilterCtrl::OnLeftDown( wxMouseEvent& event )
{
	if( event.GetX() < 21 && m_pMenu )
	{
		PopupMenu( m_pMenu, 10, 11 );
	}
}

#endif //__WXMAC__

/**********************************************************************************************/
int dgtFilterCtrl::GetSelection( void )
{
	int sel = 0;
	
	wxMenu* tMenu = GetMenu();

	if ( tMenu )
	{
		size_t i = 0;

		wxMenuItemList itemList = tMenu->GetMenuItems();

		for ( wxMenuItemList::compatibility_iterator node = itemList.GetFirst();
              node;
              node = node->GetNext() )
		{
			wxMenuItem* item = node->GetData();

			if ( item->IsChecked() )
			{
				sel = i;
				break; 
			}
			
			++i;
		}
	}
	
	return sel;
}
